package netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;

import java.nio.charset.StandardCharsets;

/**
 * @Author ZhangGJ
 * @Date 2022/02/22 15:03
 */
public class NettyClientHandle implements ChannelInboundHandler {
	@Override
	public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {

	}

	@Override
	public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception {

	}

	@Override
	public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {
		channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("客户端", StandardCharsets.UTF_8));
	}

	@Override
	public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {

	}

	@Override
	public void channelRead(ChannelHandlerContext channelHandlerContext, Object o)
			throws Exception {
		ByteBuf byteBuf = (ByteBuf) o;
		System.out.println("服务端发来消息: " + byteBuf.toString(StandardCharsets.UTF_8));
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {

	}

	@Override
	public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o)
			throws Exception {

	}

	@Override
	public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext)
			throws Exception {

	}

	@Override
	public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {

	}

	@Override
	public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable)
			throws Exception {

	}
}
